package com.cll.prototype.mongo.direct.util;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
 * 描述信息:
 * 构建客户端与Mongo Server连接的客户端的工具类
 * @author CLL
 * @version 1.0
 * @date 2020/11/29 13:44
 */
public class MongoClientUtil {

    private static final String DEFAULT_DATABASE = "prototype";
    private static final String DEFAULT_COLLECTION = "resume_preview";

    public static MongoClient getClientInstance(){
        MongoClient mongoClient = new MongoClient("192.168.0.110", 27017);
//        MongoDatabase db = mongoClient.getDatabase("prototype");
//        MongoCollection<Document> resumePreview = db.getCollection("resume_preview");

        return mongoClient;
    }

    public static MongoDatabase getDatabase(MongoClient mongoClient, String database){
        if (null == mongoClient) {
            mongoClient = getClientInstance();
        }
        if (null == database || "".equals(database)) {
            database = DEFAULT_DATABASE;
        }
        return mongoClient.getDatabase(database);
    }

    public static MongoCollection<Document> getCollection(MongoDatabase database, String collectionName){
        if (null == database) {
            database = getDatabase(null, null);
        }
        if (null == collectionName || "".equals(collectionName)) {
            collectionName = DEFAULT_COLLECTION;
        }
        return database.getCollection(collectionName);
    }

    public static void close(MongoClient mongoClient){
        mongoClient.close();
    }

}